package org.jeecg.modules.middle.index;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manage.entity.CaishuiMiddleCompany;
import org.jeecg.modules.manage.entity.CaishuiMiddleUser;
import org.jeecg.modules.manage.entity.CaishuiServiceCompany;
import org.jeecg.modules.middle.auditCenter.mapper.CaishuiTaskSettleMapper;
import org.jeecg.modules.middle.business.mapper.CaishuiServiceCompanyMapper;
import org.jeecg.modules.middle.business.service.ICaishuiMiddleUserService;
import org.jeecg.modules.middle.business.service.ICaishuiServiceCompanyService;
import org.jeecg.modules.middle.datas.mapper.CaishuiRechargeMapper;
import org.jeecg.modules.middle.datas.mapper.CaishuiRefundRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 首页统计
 * @Author: yfkj
 * @Date: 2023-02-07
 * @Version: V1.0
 */
@Api(tags = "首页统计")
@RestController
@RequestMapping("/index/statistics")
@Slf4j
public class IndexStatisticsController {

    @Resource
    private CaishuiServiceCompanyMapper caishuiServiceCompanyMapper;
    @Resource
    private CaishuiRechargeMapper caishuiRechargeMapper;
    @Resource
    private CaishuiTaskSettleMapper caishuiTaskSettleMapper;
	@Resource
	private CaishuiRefundRecordMapper caishuiRefundRecordMapper;


    /**
     * 查询所属服务公司列表
     *
     * @return
     */
    @GetMapping(value = "/queryCompanyList")
    public Result<?> queryCompanyList() {
		return Result.OK(caishuiServiceCompanyMapper.queryMiddleUserCompany());
    }

    /**
     * 查询首页统计
     *
     * @return
     */
    @GetMapping(value = "/queryIndexStatistics")
    public Result<?> queryIndexStatistics(@RequestParam(name="companyId",required=false) String companyId,
										  @RequestParam(name="dateType",required=false) String dateType,
										  @RequestParam(name="timeBegin",required=false) String timeBegin,
										  @RequestParam(name="timeEnd",required=false) String timeEnd) {
		JSONObject result = new JSONObject(3);
		//当前登录用户
		LoginUser loginUser = TokenUtils.getLoginUser();
		//公共查询条件
		Map<String, String> params = new HashMap<>();
		params.put("companyId",companyId);
		params.put("dateType",dateType);
		params.put("timeBegin",timeBegin);
		params.put("timeEnd",timeEnd);
		if(loginUser.getUserType()==2 && loginUser.getMiddleOptAuth()==2){//渠道角色
			params.put("channelBusinessId", loginUser.getChannelBusinessId());
			if(loginUser.getMiddleUserType()==2){//员工
				params.put("userId",loginUser.getId());
			}
		}

		//查询充值金额、服务费
		Map<String, String> r_params = new HashMap<>();
		r_params.put("status","1");
		params.putAll(r_params);
		Map<String,Object> r_map = caishuiRechargeMapper.queryTotalAmount(params);
		result.put("rechargeAmount", oConvertUtils.getDouble(r_map.get("amount")+"",0.00));

		//查询下发金额、服务费
		Map<String, String> w_params = new HashMap<>();
		w_params.put("status","2");
		w_params.put("extendStatus","2");
		params.putAll(w_params);
		Map<String,Object> w_map = caishuiTaskSettleMapper.queryCaishuiTaskSettleTotal(params);
		result.put("withdrawalAmount", oConvertUtils.getDouble(w_map.get("successAmount")+"",0.00));
		result.put("serviceFee",oConvertUtils.getDouble(w_map.get("successServiceFee")+"",0.00));

		//查询退款金额、退款服务费
		Map<String, String> t_params = new HashMap<>();
		t_params.put("status","4");
		params.putAll(t_params);
		Map<String,Object> t_map = caishuiRefundRecordMapper.queryRefundTotal(params);
		result.put("drawbackAmount",oConvertUtils.getDouble(t_map.get("money")+"",0.00));
		result.put("drawbackServiceCharge",oConvertUtils.getDouble(t_map.get("serviceFee")+"",0.00));
		return Result.OK(result);
    }

	/**
	 * 查询首页统计趋势
	 * @return
	 */
	@GetMapping(value = "/queryIndexChartList")
	public Result<?> queryIndexNewList(@RequestParam(name="companyId",required=false) String companyId,
									   @RequestParam(name="dayType",required=true) int dayType) {
		Map<String,Object> result = new HashMap<>();
		//当前登录用户
		LoginUser loginUser = TokenUtils.getLoginUser();
		Map<String, String> params = new HashMap<>();
		params.put("companyId",companyId);
		if(loginUser.getUserType()==2 && loginUser.getMiddleOptAuth()==2){//渠道角色
			params.put("channelBusinessId", loginUser.getChannelBusinessId());
			if(loginUser.getMiddleUserType()==2){//员工
				params.put("userId",loginUser.getId());
			}
		}
		if(dayType==1){//最近7天
			List<Map<String,Object>> last7DaysRechargeSum = caishuiRechargeMapper.last7DaysRechargeSum(params);
			List<Map<String,Object>> last7DaysTaskSettleSum = caishuiTaskSettleMapper.last7DaysTaskSettleSum(params);
			result.put("rechargeSum",last7DaysRechargeSum);
			result.put("taskSettleSum",last7DaysTaskSettleSum);
		}else if(dayType==2){//最近30天
			List<Map<String,Object>> last30DaysRechargeSum = caishuiRechargeMapper.last30DaysRechargeSum(params);
			List<Map<String,Object>> last30DaysTaskSettleSum = caishuiTaskSettleMapper.last30DaysTaskSettleSum(params);
			result.put("rechargeSum",last30DaysRechargeSum);
			result.put("taskSettleSum",last30DaysTaskSettleSum);
		}
		return Result.OK(result);
	}

	/**
	 * 查询预充值总额
	 * @return
	 */
	@GetMapping(value = "/queryPreItemsTotal")
	public Result<?> queryPreItemsTotal() {
		//当前登录用户
		LoginUser loginUser = TokenUtils.getLoginUser();
		Map<String, String> params = new HashMap<>();
		if(loginUser.getUserType()==2 && loginUser.getMiddleOptAuth()==2){//渠道角色
			params.put("channelBusinessId", loginUser.getChannelBusinessId());
			if(loginUser.getMiddleUserType()==2){//员工
				params.put("userId",loginUser.getId());
			}
		}
		return Result.OK(caishuiTaskSettleMapper.queryPreItemsTotal(params));
	}

}
